package utils;

import entities.Year;
import java.text.SimpleDateFormat;
import javax.swing.JOptionPane;

/**
 *
 * @author chocoseb
 */
public class AutoSave extends Thread {

    private Year _yearToSave;
    private boolean stopped = false;
    private ChocoBar msg;

    public AutoSave(Year yearToSave, ChocoBar msg) {
        this._yearToSave = yearToSave;
        this.msg = msg;
    }

    public void stopSave() {
        stopped = true;
    }

    @Override
    public void run() {
        try {
            while (!stopped) {
                Thread.sleep(50000);
                _yearToSave.save();
                SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");
                msg.showInformationMessage("The file has been saved at " + format.format(ChocoDate.now().getTime()));
            }
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, e.getMessage(), null, JOptionPane.ERROR_MESSAGE);
        }
    }
}
